package com.example.emos.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.common.util.R;
import com.example.emos.api.common.util.TrtcUtil;
import com.example.emos.api.controller.form.meetingform.*;
import com.example.emos.api.db.pojo.TbMeeting;
import com.example.emos.api.service.MeetingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @author keying
 * @create 2024--05--19
 */
@Slf4j
@RestController
@RequestMapping("/meeting")
@Tag(name = "meetingController",description = "会议web接口")
public class MeetingController {

    @Value("${tencent.trtc.appId}")
    private int appId;

    @Autowired
    private TrtcUtil trtcUtil;

    @Autowired
    private MeetingService meetingService;

    //线下会议

    @PostMapping("/searchOfflineMeetingByPage")
    @SaCheckLogin
    @Operation(summary = "查询线下会议分页数据")
    public R searchOfflineMeetingByPage(@Valid @RequestBody SearchOfflineMeetingByPageForm form){
        Integer page = form.getPage();
        Integer length = form.getLength();
        Integer start  = (page - 1) * length;
        HashMap hashMap = new HashMap() {{
            put("date", form.getDate());
            put("start", start);
            put("length", length);
            put("mold", form.getMold());
            put("userId", StpUtil.getLoginId());
        }};

        PageUtils pageUtils  = meetingService.searchOfflineMeetingByPage(hashMap);
        return R.ok().put("page",pageUtils);
    }

    @PostMapping("/insert")
    @Operation(summary = "添加会议")
    @SaCheckLogin
    public R insert(@Valid @RequestBody InsertMeetingForm form){
        DateTime start = DateUtil.parse(form.getDate() + " " + form.getStart());
        DateTime end = DateUtil.parse(form.getDate() + " " + form.getEnd());

        if (start.isAfterOrEquals(end)){
            return R.error("开始时间不能大于结束时间");
        }else if (new DateTime().isAfterOrEquals(start)){
            return R.error("开会时间必须在现在之后");
        }

        TbMeeting meeting = JSONUtil.parse(form).toBean(TbMeeting.class);
        meeting.setUuid(UUID.randomUUID().toString(true));
        meeting.setCreatorId(StpUtil.getLoginIdAsInt());
        meeting.setStatus((short)1);

        int rows = meetingService.insert(meeting);
        return R.ok().put("rows",rows);
    }

    @PostMapping("/recieveNotify")
    @Operation(summary = "接收工作流通知")
    public R recieveNotify(@Valid @RequestBody RecieveNotifyForm form){
        if (form.getResult().equals("同意")){
            log.debug(form.getUuid() + "的会议审批通过");
        }else{
            log.debug(form.getUuid() + "的会议审批不通过");
        }
        return R.ok();
    }

    @PostMapping("/searchOfflineMeetingInWeek")
    @Operation(summary = "查询某个会议室一周的会议")
    @SaCheckLogin
    public R searchOfflineMeetingInWeek(@Valid  @RequestBody SearchOfflineMeetingInWeekForm form){
        String date = form.getDate();
        DateTime startDate, endDate;
        if (date != null && date.length() > 0){
            startDate = DateUtil.parseDate( date);
            endDate = startDate.offsetNew(DateField.DAY_OF_WEEK,6);
        }else{
            startDate = DateUtil.beginOfWeek(new Date());
            endDate = DateUtil.endOfWeek(new Date());
        }
        HashMap map = new HashMap(){{
            put("place",form.getName());
            put("startDate",startDate.toDateStr());
            put("endDate",endDate.toDateStr());
            put("mold",form.getMold());
            put("userId",StpUtil.getLoginIdAsLong());
        }};

        ArrayList<HashMap> list = meetingService.searchOfflineMeetingInWeek(map);
        ArrayList days = new ArrayList();
        DateRange range = DateUtil.range(startDate, endDate, DateField.DAY_OF_WEEK);
        range.forEach(one ->{
            JSONObject json = new JSONObject();
            json.set("date",one.toString("MM/dd"));
            json.set("day",one.dayOfWeekEnum().toChinese("周"));
            days.add(json);
        });

        return R.ok().put("list",list).put("days",days);
    }

    @PostMapping("/searchMeetingInfo")
    @Operation(summary = "查询会议信息")
    @SaCheckLogin
    public R searchMeetingInfo(@Valid @RequestBody SearchMeetingInfoForm form){
        HashMap map = meetingService.searchMeetingInfo(form.getStatus(), form.getId());
        return R.ok(map);
    }

    @PostMapping("/deleteMeetingApplication")
    @Operation(summary = "删除会议申请")
    @SaCheckLogin
    public R deleteMeetingApplication(@Valid @RequestBody DeleteMeetingApplicationForm form){
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("userId",StpUtil.getLoginIdAsLong());
        param.put("creatorId",StpUtil.getLoginIdAsLong());

        int rows = meetingService.deleteMeetingApplication(param);
        return R.ok().put("rows",rows);
    }

    //线上会议
    @PostMapping("/searchOnlineMeetingByPage")
    @Operation(summary = "分页查询线上会议")
    @SaCheckLogin
    public R searchOnlineMeetingByPage(@Valid @RequestBody SearchOnlineMeetingByPageForm form){
        Integer page = form.getPage();
        Integer length = form.getLength();
        int start = (page-1) * length;
        HashMap param = new HashMap(){{
            put("date",form.getDate());
            put("mold",form.getMold());
            put("userId",StpUtil.getLoginId());
            put("start",start);
            put("length",length);
        }};

        PageUtils pageUtils = meetingService.searchOnlineMeetingByPage(param);
        return R.ok().put("page",pageUtils);
    }

    @GetMapping("/searchMyUserSig")
    @Operation(summary = "获取用户签名")
    @SaCheckLogin
    public R searchMyUserSig(){
        int userId = StpUtil.getLoginIdAsInt();
        String userSig = trtcUtil.genUserSig(userId+"");
        return R.ok().put("userSig",userSig).put("userId",userId).put("appId",appId);
    }

    @PostMapping("/searchRoomIdByUUID")
    @Operation(summary = "查询会议房间的RoomId")
    public R searchRoomIdByUUID(@Valid @RequestBody SearchRoomIdByUUIDForm form){
        Long roomId = meetingService.searchRoomIdByUUID(form.getUuid());
        return R.ok().put("roomId",roomId);
    }

    @PostMapping("/searchOnlineMeetingMembers")
    @Operation(summary = "查询线下会议成员")
    @SaCheckLogin
    public R searchOnlineMeetingMembers(@Valid @RequestBody SearchOnlineMeetingMembersForm form){
        HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
        param.put("userId",StpUtil.getLoginIdAsInt());
        ArrayList<HashMap> list = meetingService.searchOnlineMeetingMembers(param);
        return R.ok().put("list",list);
    }


}
